﻿using System;
using Less.Core.Model;
using Less.DAL;

namespace Less.Core.Loggers
{
    /// <summary>
    /// Logger that writes to the lessFramework DataBase 
    /// </summary>
    public class DatabaseLogger : BaseLogger
    {
        public DatabaseLogger()
        {
            MinSeverity = EntrySeverities.Metric;
        }

        /// <summary>
        /// DataBase writer method. 
        /// </summary>
        /// <param name="entrySeverity"></param>
        /// <param name="entryMessage"></param>
        /// <param name="entryCategory"></param>
        /// <param name="entrySource"></param>
        /// <param name="userName"></param>
        /// <param name="processId"></param>
        /// <param name="metricName"></param>
        /// <param name="metricValue"></param>
        /// <returns></returns>
        public override bool Write(EntrySeverities entrySeverity, string entryMessage, EntryCategories entryCategory = EntryCategories.Base, string entrySource = "", string userName = "", long processId = 0, string metricName = "", object metricValue = null)
        {
            // Do not write LessDatabase related messages, to avoid circular calling
            if (entryCategory == EntryCategories.LessDb) return false;
            var logEntry = LogEntry.CreateLogEntry(entrySeverity, entryMessage, entryCategory, entrySource, userName, processId, metricName, metricValue);
            try
            {
                LogEntriesDAL dal = new LogEntriesDAL();
                dal.Insert(logEntry);
                return true;
            }
            catch (Exception e)
            {
                Log.Fatal(e, EntryCategories.LessDb);
                return false;
            }
        }
    }
}
